package com.moco.launcher.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.moco.launcher.R;
import com.moco.launcher.listener.PopWindowCallback;
import com.moco.launcher.view.CircleImageView;
import com.oazon.common.Logger;

public class ViewPagerAdapter extends PagerAdapter {
	private static final String TAG = "ViewPagerAdapter";
	private LayoutInflater inflater;
	private Context mContext;

	private PopWindowCallback mPopWindowCallback = null;

	private static View saveView = null;

	private static View firstView = null;
	private static View lastView = null;

	public void registerPopWindowCallback(PopWindowCallback cb) {
		this.mPopWindowCallback = cb;
	}

	public ViewPagerAdapter(Context context) {
		mContext = context;
		inflater = LayoutInflater.from(mContext);
	}

	@Override
	public void destroyItem(final ViewGroup container, final int position,
			final Object object) {
	}

	@Override
	public int getCount() {
		return 1;
	}

	public View getCurrentView() {
		return saveView;
	}

	public View getFirstView() {
		return firstView;
	}

	public View getLastView() {
		return lastView;
	}

	@Override
	public Object instantiateItem(ViewGroup view, final int position) {
		//防止多次创建View,导致内存溢出 ---Gavin 15.7.3
		if (firstView != null && lastView != null && saveView != null) {
			return view;
		}
		Logger.d(TAG, "position====position");
		final View imageLayout = inflater.inflate(R.layout.verticalitem_layout,
				view, false);

		final CircleImageView cView = (CircleImageView) imageLayout
				.findViewById(R.id.circleimageview);
		cView.setDrawingCacheQuality(50);
		cView.setDrawingCacheEnabled(true);

		ImageView popImage = (ImageView) imageLayout
				.findViewById(R.id.up_pop_image);
		popImage.setDrawingCacheEnabled(true);

		imageLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mPopWindowCallback != null) {
					// mPopWindowCallback.show(); //去掉多功能界面 disable 06.27
				}
			}
		});

		if (position == Integer.MIN_VALUE) {
			view.addView(imageLayout);
			firstView = imageLayout;
			return imageLayout;

		} else if (position == Integer.MAX_VALUE) {
			view.addView(imageLayout);
			lastView = imageLayout;
			return imageLayout;
		}
		view.addView(imageLayout);
		saveView = imageLayout;

		return imageLayout;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}
}